﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading.Tasks;

namespace FaceRecognition
{
    class Sampler
    {
        private List <long> _samplesVec = null;
        private string _filePath;
        private int _samplesCount = 0;
        private int _maxVal;
        private int[,] _A;

        public string filePath
        {
            get
            {
                return _filePath;
            }
            set
            {
                _filePath = value.ToString();
            }
        }
        public int samplesCount
        {
            get
            {
                return _samplesCount;
            }
            set
            {
                _samplesCount = value;
            }
        }

        public Sampler(string fPath, int samples, int maxVal)
        {
            _samplesCount = samples;
            _maxVal = maxVal;
            _samplesVec = new List<long>();

            if (File.Exists(fPath))
            {
                String file = fPath;
                StreamReader dataStream = new StreamReader(file);

                string datasample;

                while ((datasample = dataStream.ReadLine()) != null)
                {
                    _samplesVec.Add(int.Parse(datasample));
                }
            }
            else
            {
                genFileRandomVec(fPath, _samplesCount);        
            }
        }

        public void genFileRandomVec(string fPath, long max) 
        {
            Random rn = new Random();
            long val;
            System.IO.StreamWriter file = new System.IO.StreamWriter(fPath);
            
            

            for (int i = 0; i < _samplesCount; i++)
            {
                val = rn.Next(_maxVal);
                while (_samplesVec.Contains(val))
                {
                    val = rn.Next(_maxVal);
                }
                _samplesVec.Add(val);
            }

            _samplesVec.Sort();

            foreach (Object i in _samplesVec)
            {
                file.WriteLine(i);
            }

            file.Close();    
        }

        public int [,] genFileDataBase(string fPath, string [] files)
        {
            PGM sam = new PGM();
            _A = new int[_samplesCount, files.Length];
            long[] counts = new long[_samplesCount];
            int[] AImag = new int[_samplesCount * files.Length];

            int[] image;
            int len,mColor, mWidth,n = 0,j = 0, size;
            string  mType;
            bool flag = false;
            
            for (int i = 0; i < _samplesCount; i++)
            {
                for (j = 0; j < files.Length; j++)
                {
                    _A[i, j] = -1;
                }
            }

            counts = _samplesVec.ToArray();

            j = 0;
            foreach (string k in files)
            {
                image = PGM.ReadPGM(k,out len, out mWidth, out mColor,out mType);
                size = mWidth * len;
                
                if(mWidth != 168 || len != 192)
                {
                    //Console.WriteLine(k);
                }

                for (int i = 0; i < size; i++)
                {

                    if (counts.Contains(i))
                    {
                        _A[j++, n] = image[i];
                    }
                
                }
                j = 0;
                n++;
            }

            long counter = 0;

            for (int i = 0; i < _samplesCount; i++)
            {
                for (j = 0; j < files.Length; j++)
                {
                    AImag[counter++] = _A[i,j];
                }
            }

            /*sam.WritePGM("D:\\szukane.pgm", _samplesCount, files.Length, 255, "P5", AImag);
            
            System.IO.StreamWriter sr = new System.IO.StreamWriter(@"D:\WriteLines2.txt", true);

            string str = "";
            for (int i = 0; i < _samplesCount; i++)
            {
                for (j = 0; j < files.Length; j++)
                {
                    sr.Write(A[i, j].ToString() + " ");
                }
                sr.Write("\n");
                str = "";
            }

            sr.Close();*/
            return _A;
        }
    }
}
